<template>
  <div class="pie-chart">
    <div ref="echartRef" style="width: 100%; height: 180px" />
  </div>
</template>
<script lang="ts" setup>
import * as echarts from 'echarts'

const echartRef: any = ref(null)
const initPieChart = (data) => {
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '5%',
      left: '0',
      orient: 'vertical'
    },
    series: [
      {
        name: '',
        type: 'pie',
        left: '20%',
        radius: ['30%', '70%'],
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data
      }
    ]
  }
  echartRef.value.setOption(option)
}
const initBarChart = (keys, values) => {
  const option = {
    xAxis: {
      type: 'category',
      data: keys
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: values,
        type: 'bar'
      }
    ]
  }
  console.log(option)

  echartRef.value.setOption(option)
}

onMounted(() => {
  echartRef.value = echarts.init(echartRef.value)
})

//导出给refs使用
defineExpose({ initPieChart, initBarChart })
</script>

<style scoped lang="scss"></style>
